package com.cmrx.model.service.impl;

import com.cmrx.model.dao.InvestigationDao;
import com.cmrx.model.dao.ScenePictureDao;
import com.cmrx.model.entity.CommonPicture;
import com.cmrx.model.entity.Photo;
import com.cmrx.model.entity.PictureInfo;
import com.cmrx.model.entity.User;
import com.cmrx.model.service.ScenePictureService;
import com.cmrx.model.util.Html2ImageUtil;
import com.cmrx.model.util.ImgUtil;
import com.cmrx.model.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ScenePictureServiceImpl implements ScenePictureService {

    @Autowired
    private ScenePictureDao scenePictureDao;
    @Autowired
    private InvestigationDao investigationDao;


    @Override
    public String addPictuer(Map<String, String> map, User user) {
        //COMMON_PICTURE (通用图片表),ID,INVESTIGATION_ID,CONTENT,WIDTH,HEIGHT,FILE_NAME,CATEGORY,SUB_CATEGORY,TYPE,DESCRIPTION,CREATE_USER,CREATE_DATETIME
        Date date = new Date();
        SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sFormat.format(date);

        String title =  map.get("title");                                   //标题         title
        String occurrence_date_from =  map.get("occurrence_date_from").split(" ")[0];     //案发时间      occurrenceDateFrom
        String scene_detail = map.get("scene_detail");                      //案发地址      sceneDetail
        String draw_organize = map.get("draw_organize");                    //绘画人单位    drawOrganize
        String draw_person = map.get("draw_person");                        //绘画人姓名    drawPerson
        String draw_time = map.get("draw_time").split(" ")[0];                            //绘画人时间    drawTime
        String content = map.get("content");                                //图片内容base64 content
        PictureInfo pictureInfo = new PictureInfo();
        pictureInfo.setTitle(title);
        pictureInfo.setOccurrence_date_from(occurrence_date_from);
        pictureInfo.setScene_detail(scene_detail);
        pictureInfo.setDraw_organize(draw_organize);
        pictureInfo.setDraw_person(draw_person);
        pictureInfo.setDraw_time(draw_time);

//        Map<String,Object> param =  Html2ImageUtil.htm2Img(content,pictureInfo);
        Map<String,Object> param = ImgUtil.drawImage(content,pictureInfo);

        String investigationId =  map.get("investigationid");                  //现勘id investigationId
        String commonPictureId = UUIDUtil.getUUID();
        param.put("commonPictureId",commonPictureId);
        param.put("investigationId",investigationId);
        param.put("createUser",draw_person);
        param.put("createDateTime",time);
        param.put("category",1);
        param.put("fileName",title);

        scenePictureDao.addPictuer(param);

        //SCENE_PICTURE （现场图信息表）ID,SERIAL_NO,INVESTIGATION_ID,CREATE_USER_ID,PICTURE_ID,PICTURE_NAME,PICTURE_TYPE,DESCRIPTION,CREATE_USER,CREATE_DATETIME
        param.put("scenePictureId",UUIDUtil.getUUID());
        int count = scenePictureDao.selectCountScenePicture(investigationId);
        count = count +1;
        param.put("serialNo",count);
        param.put("userid",map.get("userid"));
        param.put("pictureType","1082");

        scenePictureDao.addScenePictuer(param);

        //修改经纬度
        //维度
        param.put("reserver4",map.get("reserver4"));
        //经度
        param.put("reserver6",map.get("reserver6"));
        investigationDao.updateLawCaseReserver(param);

        //压缩图片处理。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
        param.put("commonPictureThumId",UUIDUtil.getUUID());
        scenePictureDao.addPictureThum(param);

        return Base64.getEncoder().encodeToString((byte[]) param.get("content"));
    }

    @Override
    public List<Map<String, Object>> selectPictuer(Map<String, String> map) {
        List<Map<String, Object>> mapList = scenePictureDao.selectPictuer(map);
        for (Map<String,Object> result:mapList) {
          byte [] b = (byte[]) result.get("content");
          result.put("content",Base64.getEncoder().encodeToString(b));
        }
        return mapList;
    }

    @Override
    public void deletePictuer(Map<String, String> map) {
        scenePictureDao.deleteCommonPictuer(map);
        scenePictureDao.deleteScenePictuer(map);
        scenePictureDao.deleteCommonPictuerThum(map);
    }

    @Override
    public List<Map<String, Object>> selectPhoto(Map<String, String> map) {
        List<Map<String, Object>> list =  scenePictureDao.selectPhoto(map);
        for (Map<String,Object> result:list) {
            byte [] b =(byte[]) result.get("content");
            result.put("content",Base64.getEncoder().encodeToString(b));
        }
        return list;
    }

    @Override
    public void deletePhoto(Map<String, String> map) {
        scenePictureDao.deleteCommonPictuer(map);
        scenePictureDao.deleteScenePhoto(map);
        scenePictureDao.deleteCommonPictuerThum(map);
    }

    @Override
    public Map<String, String> addorupdatePhoto(Map<String, String> map, User user) {
//                "id":"",
//                "content":"",
//                "width":"",
//                "height":"",
//                "file_name":"",
//                "type":"png",
//                "create_user":"",
//                "update_user":"",
//                "create_datetime":"",
//                "update_datetime":"",
//                "description":""
        //investigationid
        //token
        String id = map.get("id");
        if(!StringUtils.isEmpty(id)){
            //修改
            scenePictureDao.updateCommonPicture(map);
            scenePictureDao.updatePhoto(map);
        }else {
            //新增
            id = UUIDUtil.getUUID();
            String investigationid = map.get("investigationid");
            CommonPicture picture = new CommonPicture();
            picture.setId(id);
            picture.setInvestigationId(investigationid);
            byte[] b = Base64.getDecoder().decode(map.get("content"));
            picture.setContent(b);
            picture.setWidth(map.get("width"));
            picture.setHeigth(map.get("height"));
            picture.setFileName(map.get("file_name"));
            picture.setCategory("2");
            picture.setType(map.get("type"));
            picture.setCreateUser(map.get("create_user"));
            picture.setCreateDateTime(map.get("create_datetime"));
            picture.setUpdateUser(map.get("update_user"));
            picture.setUpdateDateTime(map.get("update_datetime"));
            scenePictureDao.insertPicture(picture);
            int count = scenePictureDao.selectCountPhoto(investigationid);
            Photo photo = new Photo();
            photo.setId(UUIDUtil.getUUID());
            photo.setSerialNo(""+(count++));
            photo.setInvestigationId(investigationid);
            photo.setCreateUserId(map.get("userid"));
            photo.setPhotoId(id);
            photo.setPhotoName(map.get("file_name"));
            photo.setPhotoType(map.get("photo_type"));
            photo.setDescription(map.get("description"));
            photo.setCreateUser(map.get("create_user"));
            photo.setCreateDatetime(map.get("create_datetime"));
            photo.setUpdateUser(map.get("update_user"));
            photo.setUpdateDatetime(map.get("update_datetime"));
            scenePictureDao.insertPhoto(photo);
            map.put("id",id);
            //压缩图处理

            Map<String,Object> param = new HashMap<>();
            param.put("commonPictureThumId",UUIDUtil.getUUID());
            param.put("createDateTime",map.get("create_datetime"));
            param.put("createUser",map.get("create_user"));
            param.put("suobyte",Html2ImageUtil.createThinPicture(b));
            param.put("commonPictureId",id);
            scenePictureDao.addPictureThum(param);
        }
        return map;
    }

    @Override
    public Map<String, Object> selectOriginalPicture(String id) {
        return scenePictureDao.selectOriginalPicture(id);
    }
}
